package com.baizhi.controller;


import com.baizhi.dto.PageDTO;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.vo.CommonQueryPageVO;
import com.baizhi.vo.CommonVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    UserService userService;

   /* @RequestMapping("queryAllPage")
    public HashMap<String,Object> queryAllPage(Integer page,Integer pageSize){

        log.info("当前页数：{}",page);
        log.info("每页展示条数：{}",pageSize);

        HashMap<String, Object> map = userService.queryAllPage(page, pageSize);
        return map;
    }*/

    /*@RequestMapping("queryAllPage/{page}/{pageSize}")
    public HashMap<String,Object> queryAllPage(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize){

        log.info("当前页数：{}",page);
        log.info("每页展示条数：{}",pageSize);

        HashMap<String, Object> map = userService.queryAllPage(page, pageSize);
        return map;
    }*/

    /*@RequestMapping("queryAllPage")
    public HashMap<String,Object> queryAllPage(@RequestBody PageDTO pageDTO){

        log.info("当前页数：{}",pageDTO.getPage());
        log.info("每页展示条数：{}",pageDTO.getPageSize());

        HashMap<String, Object> map = userService.queryAllPage(pageDTO.getPage(), pageDTO.getPageSize());
        return map;
    }*/

    @RequestMapping("queryAllPage")
    public CommonQueryPageVO queryAllPage(@RequestBody PageDTO pageDTO){

        log.info("当前页数：{}",pageDTO.getPage());
        log.info("每页展示条数：{}",pageDTO.getPageSize());

        CommonQueryPageVO commonQueryPageVO = userService.queryAllPages(pageDTO.getPage(), pageDTO.getPageSize());
        return commonQueryPageVO;
    }

    /*@RequestMapping("update")
    public HashMap<String,Object> update(@RequestBody User user){
        log.info("修改用户数据：{}",user);
        return userService.update(user);
    }*/

    @RequestMapping("update")
    public CommonVO update(@RequestBody User user){
        log.info("修改用户数据：{}",user);
        return userService.updates(user);
    }

    @RequestMapping("add")
    public CommonVO add(@RequestBody User user){
        log.info("添加用户用户数据：{}",user);

        try {
            userService.add(user);
            return CommonVO.success("添加成功！！1");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.success("添加失败！！1");
        }
    }

    @RequestMapping("uploadHeadImg")
    public HashMap<String, String> uploadHeadImg(MultipartFile headImg){
        log.info("文件名：{}",headImg.getOriginalFilename());
        log.info("文件大小：{}",headImg.getSize());
        log.info("文件类型：{}",headImg.getContentType());

        HashMap<String, String> map = new HashMap<>();

        String msg = userService.uploadHeadImgAliyun(headImg);
        map.put("fileName",msg);

        return map;
    }
    @RequestMapping("queryById")
    public User queryById(String id){
        log.info("id：{}",id);
        return userService.queryById(id);
    }


    @RequestMapping("delete")
    public CommonVO delete(@RequestBody User user){
        log.info("删除用户用户数据：{}",user);

        try {
            userService.delete(user);
            return CommonVO.success("删除成功！！1");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.success("删除失败！！1");
        }
    }
}
